<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="账目名称" prop="accountName">
        <el-input
          v-model="queryParams.accountName"
          placeholder="请输入账目名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['bank:bank:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['bank:bank:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['bank:bank:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['bank:bank:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="bankList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="主键ID" align="center" prop="id" />
      <el-table-column label="账户编号" align="center" prop="accountId" />
      <el-table-column label="账目名称" align="center" prop="accountName" />
      <el-table-column label="银行" align="center" prop="bank" />
      <el-table-column label="银行账号" align="center" prop="bankAccount" />
      <el-table-column label="币别" align="center" prop="currency" />
      <el-table-column label="会计科目" align="center" prop="accountingAccount" />
      <el-table-column label="绑定银企互联" align="center" prop="Interconnect" />
      <el-table-column label="绑定日期" align="center" prop="bindTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.bindTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="到期日期" align="center" prop="expireTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.expireTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="启用状态" align="center" prop="accountStatus" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['bank:bank:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['bank:bank:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改银行存款对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="账户编号" prop="accountId">
          <el-input v-model="form.accountId" placeholder="请输入账户编号" />
        </el-form-item>
        <el-form-item label="账目名称" prop="accountName">
          <el-input v-model="form.accountName" placeholder="请输入账目名称" />
        </el-form-item>
        <el-form-item label="银行" prop="bank">
          <el-input v-model="form.bank" placeholder="请输入银行" />
        </el-form-item>
        <el-form-item label="银行账号" prop="bankAccount">
          <el-input v-model="form.bankAccount" placeholder="请输入银行账号" />
        </el-form-item>
<!--        <el-form-item label="币别" prop="currency">-->
<!--          <el-input v-model="form.currency" placeholder="请输入币别" />-->
<!--        </el-form-item>-->
<!--        <el-form-item label="会计科目" prop="accountingAccount">-->
<!--          <el-input v-model="form.accountingAccount" placeholder="请输入会计科目" />-->
<!--        </el-form-item>-->

        <el-form-item label="币别" prop="currency">
          <!--          <el-input v-model="form.currency" placeholder="请输入币别" />-->
          <el-select v-model="form.currency" placeholder="请选择币别">
            <el-option
              v-for="item in options"
              :key="item.value"
              :label="item.label"
              :value="item.value">
            </el-option>
          </el-select>
        </el-form-item>

        <el-form-item label="会计科目" prop="accountingAccount" >
          <el-select v-model="form.accountingAccount" placeholder="请选择科目" >
            <el-option
              v-for="item in tableData"
              :key="item.subjectId"
              :label="item.subjectName"
              :value="item.subjectId">
            </el-option>
          </el-select>

          <el-popover
            placement="right"
            width="400"
            trigger="click">

            <el-tabs v-model="activeName" @tab-click="handleClick" >
              <el-tab-pane label="全部" name="first">

                <el-table
                  ref="singleTable"
                  :data="tableData"
                  highlight-current-row
                  @current-change="handleCurrentChange"
                  style="width: 100%"
                  @row-dblclick="AddData"
                >
                  <el-table-column
                    type="index"
                    width="50">
                  </el-table-column>
                  <el-table-column
                    property="subjectId"
                    label="项目编号">
                  </el-table-column>
                  <el-table-column
                    property="subjectName"
                    label="项目名称">
                  </el-table-column>
                </el-table>

              </el-tab-pane>
              <el-tab-pane label="资产" name="second">

                <el-table
                  ref="singleTable"
                  :data="tableData"
                  highlight-current-row
                  @current-change="handleCurrentChange"
                  style="width: 100%"
                  @row-dblclick="AddData">
                  <el-table-column
                    type="index"
                    width="50">
                  </el-table-column>
                  <el-table-column
                    property="subjectId"
                    label="项目编号">
                  </el-table-column>
                  <el-table-column
                    property="subjectName"
                    label="项目名称">
                  </el-table-column>
                </el-table>

              </el-tab-pane>
              <el-tab-pane label="负债" name="third">

                <el-table
                  ref="singleTable"
                  :data="tableData"
                  highlight-current-row
                  @current-change="handleCurrentChange"
                  style="width: 100%"
                  @row-dblclick="AddData">
                  <el-table-column
                    type="index"
                    width="50">
                  </el-table-column>
                  <el-table-column
                    property="subjectId"
                    label="项目编号">
                  </el-table-column>
                  <el-table-column
                    property="subjectName"
                    label="项目名称">
                  </el-table-column>
                </el-table>

              </el-tab-pane>
              <el-tab-pane label="权益" name="three">

                <el-table
                  ref="singleTable"
                  :data="tableData"
                  highlight-current-row
                  @current-change="handleCurrentChange"
                  style="width: 100%"
                  @row-dblclick="AddData">
                  <el-table-column
                    type="index"
                    width="50">
                  </el-table-column>
                  <el-table-column
                    property="subjectId"
                    label="项目编号">
                  </el-table-column>
                  <el-table-column
                    property="subjectName"
                    label="项目名称">
                  </el-table-column>
                </el-table>

              </el-tab-pane>
              <el-tab-pane label="成本" name="fourth">

                <el-table
                  ref="singleTable"
                  :data="tableData"
                  highlight-current-row
                  @current-change="handleCurrentChange"
                  style="width: 100%"
                  @row-dblclick="AddData">
                  <el-table-column
                    type="index"
                    width="50">
                  </el-table-column>
                  <el-table-column
                    property="subjectId"
                    label="项目编号">
                  </el-table-column>
                  <el-table-column
                    property="subjectName"
                    label="项目名称">
                  </el-table-column>
                </el-table>

              </el-tab-pane>
              <el-tab-pane label="损益" name="six">

                <el-table
                  ref="singleTable"
                  :data="tableData"
                  highlight-current-row
                  @current-change="handleCurrentChange"
                  style="width: 100%"
                  @row-dblclick="AddData">
                  <el-table-column
                    type="index"
                    width="50">
                  </el-table-column>
                  <el-table-column
                    property="subjectId"
                    label="项目编号">
                  </el-table-column>
                  <el-table-column
                    property="subjectName"
                    label="项目名称">
                  </el-table-column>
                </el-table>

              </el-tab-pane>
            </el-tabs>

            <el-button slot="reference">成本</el-button>

          </el-popover>
        </el-form-item>


        <el-form-item label="绑定银企互联" prop="Interconnect">
          <el-input v-model="form.Interconnect" placeholder="请输入绑定银企互联" />
        </el-form-item>
        <el-form-item label="绑定日期" prop="bindTime">
          <el-date-picker clearable
            v-model="form.bindTime"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择绑定日期">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="到期日期" prop="expireTime">
          <el-date-picker clearable
            v-model="form.expireTime"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择到期日期">
          </el-date-picker>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listBank, getBank, delBank, addBank, updateBank } from "@/api/bank/bank";
import {subjectListCash} from "@/api/cash/cash";

export default {
  name: "Bank",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 银行存款表格数据
      bankList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        accountName: null,
      },
      options: [{
        value: '人民币',
        label: '人民币'
      }, {
        value: '美元',
        label: '美元'
      }, ],
      value: '',
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        accountId: [
          { required: true, message: "账户编号不能为空", trigger: "blur" }
        ],
        accountName: [
          { required: true, message: "账目名称不能为空", trigger: "blur" }
        ],
        bank: [
          { required: true, message: "银行不能为空", trigger: "blur" }
        ],
        currency: [
          { required: true, message: "币别不能为空", trigger: "blur" }
        ],
      },
      activeName: 'first',
      tableData: [],
      currentRow: null,
      index :0,
    };
  },
  created() {
    this.getList();
    this.showAccpt(0);
  },
  methods: {

    AddData(row){
      this.form.accountingAccount = row.subjectName
    },

    showAccpt(id){
      subjectListCash(id).then(response => {
        this.tableData = response.rows;
        console.log("tableData"+this.tableData)
      });
    },

    setCurrent(row) {
      this.$refs.singleTable.setCurrentRow(row);
      console.log("setCurrent"+row);
    },

    handleCurrentChange(val) {
      this.currentRow = val;
      console.log("currentRow"+val);
    },

    handleClick(tab, event) {
      console.log("tab------"+tab.index);
      console.log("event------"+event);
      this.index = tab.index;
      subjectListCash(tab.index).then(response => {
        this.tableData = response.rows;
        console.log("tableData"+this.tableData)
      });
    },

    /** 查询银行存款列表 */
    getList() {
      this.loading = true;
      listBank(this.queryParams).then(response => {
        this.bankList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        accountId: null,
        accountName: null,
        bank: null,
        bankAccount: null,
        currency: null,
        accountingAccount: null,
        Interconnect: null,
        bindTime: null,
        expireTime: null,
        accountStatus: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加银行存款";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getBank(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改银行存款";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateBank(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addBank(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除银行存款编号为"' + ids + '"的数据项？').then(function() {
        return delBank(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('bank/bank/export', {
        ...this.queryParams
      }, `bank_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
